package mini_game_fx;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import javafx.event.Event;
import javafx.event.EventDispatchChain;
import javafx.event.EventDispatcher;
import javafx.event.EventHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;

/**
 * This is a JavaFX version of Minigame Framework EventRelayer class.
 *
 * The original MiniGameEventRelayerFx class has three event listeners, and
 * it dispatches events to each listeners.
 *
 * @author Heecheon Kim
 * @author Taewan Kim
 * @author Jaewoong Shin
 *
 */
public class MiniGameEventRelayerFx implements EventHandler {
    // Event relayers for key, mouse drag and mouse event.
    private MiniGameKeyEventRelayerFx keyEventRelayer;
    private MiniGameMouseDragEventRelayerFx mouseDragEventRelayer;
    private MiniGameMouseEventRelayerFx mouseEventRelayer;
    
    // Event dispatcher distinguishes each event and it distribute the events
    // to the each event relayers.
    private EventDispatcher eventDispatcher;

    /**
     * The constructor just sets up access to the game for which to respond to.
     *
     * @param initGame the game being played.
     */
    public MiniGameEventRelayerFx(MiniGameFx initGame) {
        // Initialize all event relayers.
        keyEventRelayer = new MiniGameKeyEventRelayerFx(initGame);
        mouseDragEventRelayer = new MiniGameMouseDragEventRelayerFx(initGame);
        mouseEventRelayer = new MiniGameMouseEventRelayerFx(initGame);
        
        // Initialize an event dispatcher.
        eventDispatcher = new EventDispatcher() {
            
            public Event dispatchEvent(Event event, EventDispatchChain edc) {
//                System.out.println(event.getEventType().getName());
                // Give the event to key event relayer.
                if(event instanceof KeyEvent) {
                    keyEventRelayer.handle((KeyEvent) event);
                }
                // Give the event to the mouse drag relayer.
                else if(event instanceof MouseDragEvent) {
                    mouseDragEventRelayer.handle((MouseDragEvent) event);
                }
                // Give the event to the mouse relayer.
                else if(event instanceof MouseEvent) {
//                    System.out.println("mouseeventRelayer handler firing");
                    mouseEventRelayer.handle((MouseEvent) event);
                    
                }
                
                return null;
            }
        };
    }
    
    /**
     * It returns event dispatcher.
     * 
     * @return Event dispatcher.
     */
    public EventDispatcher getEventDispatcher() {
        return eventDispatcher;
    }

    @Override
    public void handle(Event event) {
        eventDispatcher.dispatchEvent(event, null);
    }
}